package com.fintech.third.service;


import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dragon.common.util.PropertiesLoadUtil;
import com.fintech.common.entity.ResultData;
import com.fintech.common.enums.ReturnCode;
import com.fintech.third.dao.RealNameAuthenticationMapper;
import com.fintech.user.entity.RealNameAuthentication;
import com.fintech.user.service.RealNameAuthenticationService;
import com.marvels.common.utils.MarvelsLogUtil;

@Service(value="realNameAuthenticationService")
public class RealNameAuthenticationServiceImpl implements RealNameAuthenticationService {
	@Autowired
	private RealNameAuthenticationMapper realNameAuthenticationMapper;
	
	@Override
	public ResultData<Boolean> addRealNameAuthentication(RealNameAuthentication realNameAuthentication) {
		ResultData<Boolean> resultData = new ResultData<>();
		resultData.setData(true);
		if(realNameAuthentication.getUserId()==null||StringUtils.isEmpty(realNameAuthentication.getIdentityCardNo()) ||
		   StringUtils.isEmpty(realNameAuthentication.getAddress())||StringUtils.isEmpty(realNameAuthentication.getNation())||
		   StringUtils.isEmpty(realNameAuthentication.getRealName())||realNameAuthentication.getSex()==null||
		   StringUtils.isEmpty(realNameAuthentication.getAuthority())|| 
		   realNameAuthentication.getValidityfrom()==null||realNameAuthentication.getValidityto()==null||
		   StringUtils.isEmpty(realNameAuthentication.getIdcardimg1())||StringUtils.isEmpty(realNameAuthentication.getIdcardimg2()))
		{
			resultData.setErrorMessage("信息不完整!");
			return resultData;
		}
		try {
			if(realNameAuthenticationMapper.insertSelective(realNameAuthentication)!=1){
				resultData.setResultCode(ReturnCode.fail.getErrorCode());
				resultData.setData(false);
				return resultData;
			}
		}
		catch (Exception e) {
			//e.printStackTrace();
			resultData.setResultCode(ReturnCode.fail.getErrorCode());
			resultData.setData(false);
			//return resultData;
			throw e;
		}
		return resultData;
	}

	@Override
	public ResultData<Boolean> selectByUserId(Integer userId) {
		ResultData<Boolean> resultData = new ResultData<Boolean>();
		resultData.setData(true);
		try {
			RealNameAuthentication realNameAuthentication = realNameAuthenticationMapper.selectByUserId(userId);
			if(realNameAuthentication==null){
				resultData.setData(false);
			}
		} catch (Exception e) {
			resultData.setData(false);
			resultData.setMessage("查询失败!");
			e.printStackTrace();
		}
		return resultData;
	}

	@Override
	public RealNameAuthentication queryByUserId(Integer userId) {
		MarvelsLogUtil.getInstance().info("==================RealNameAuthenticationServiceImpl.queryByUserId,userId=" + userId);
		RealNameAuthentication realNameAuthentication = realNameAuthenticationMapper.selectByUserId(userId);
		return realNameAuthentication;
	}

	@Override
	public RealNameAuthentication querByIdentiyCardNo(String identiyCardNo) {
		return realNameAuthenticationMapper.querByIdentiyCardNo(identiyCardNo);
	}

	@Override
	public void deleteById(Integer id) {
		realNameAuthenticationMapper.deleteById(id);
	}

	@Override
	public Integer findByUserId(Integer userId) {
		Integer res = 1;
		RealNameAuthentication entity = realNameAuthenticationMapper.selectByUserId(userId);
		String checkDateStr = PropertiesLoadUtil.getPropertiesValue("face_check_date", "common.properties");
		SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date checkDate = null;
		try {
			checkDate = fmt.parse(checkDateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		if(null==entity){
			res = 1;
		}else if(entity.getCreatedDate().getTime()<checkDate.getTime()){//实名认证在人脸识别上线之前，就算人脸识别过了
			res = 0;
		}else if(StringUtils.equals("0", entity.getFaceTag())){//未人脸识别通过
			res= 2;
		}
		else if(StringUtils.equals("1", entity.getFaceTag())){//人脸识别通过
			res = 0;
		}
		return res;
	}

	@Override
	public void update(RealNameAuthentication entity) {
		realNameAuthenticationMapper.updateByPrimaryKeySelective(entity);
	}
	
}
